﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using XclusiveService.Models;

namespace XclusiveService.Controllers
{
    public class ItemListController : ApiController
    {
        static readonly IItemListRepository repository = new ItemListRepository();

        // Retrieve Resource
        public IEnumerable<ItemList> GetAllItemList()
        {
            return repository.GetAll();
        }

        public ItemList GetItemList(int id)
        {
            ItemList item = repository.GetById(id);
            if (item == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return item;
        }

        // Create Resource
        public HttpResponseMessage PostItemList(ItemList item)
        {
            item = repository.Add(item);
            var response = Request.CreateResponse<ItemList>(HttpStatusCode.Created, item);

            string uri = Url.Link("DefaultApi", new { id = item.listID });
            response.Headers.Location = new Uri(uri);
            return response;
        }

        // Update Resource
        public void PutItemList(int id, ItemList ItemList)
        {
            ItemList.listID = id;
            if (!repository.Update(ItemList))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        // Delete Resource
        public HttpResponseMessage DeleteItemList(int id)
        {
            repository.Remove(id);
            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }
    }
}
